/**
 * Created by Freeman on 2016/8/4.
 */
import {LOGIN_SUCCESS,GET_USERINFO_SUCCESS,GET_USERINFO_FAILURE,LOGOUT_SUCCESS,UPDATE_USER_SUCCESS} from '../constants/ActionTypes'
import {getCookie} from '../utils/authService'
const initialState = {
    token:getCookie('token')|| null,
    user:null
};

export default function auth(state =initialState,action={}) {
    switch (action.type){
        case LOGIN_SUCCESS:
            return {
                ...state,
                token:action.token
            };
        case GET_USERINFO_SUCCESS:
            return {
                ...state,
                user:action.json
            };
        case GET_USERINFO_FAILURE:
            return {
                ...state,
                user:null
            };
        case LOGOUT_SUCCESS:
            return {
                ...state,
                token:null,
                user:null
            };
        case UPDATE_USER_SUCCESS:
            return {
                ...state,
                user:action.user
            };
        default :
            return state;
    }
}